package ace.module.captcha.core.impl.manager.impl;

import ace.cmp.core.constants.CoreConstant;
import ace.cmp.data.spring.data.jdbc.querydsl.rsql.core.manager.spring.AbstractSpringDtoManager;
import ace.cmp.i18n.api.MessageSourceAssertService;
import ace.module.captcha.api.model.dto.CaptchaComponentConfigDto;
import ace.module.captcha.core.impl.dao.entity.CaptchaComponentConfig;
import ace.module.captcha.core.impl.manager.CaptchaComponentConfigManager;
import ace.module.captcha.core.impl.manager.repository.CaptchaComponentConfigMgrRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 验证码组件配置 spring data jdbc querydsl rsql manager implement.
 *
 * @author Caspar
 */
@AllArgsConstructor
@Component
@Slf4j
public class CaptchaComponentConfigManagerImpl
    extends AbstractSpringDtoManager<CaptchaComponentConfig, Long, CaptchaComponentConfigDto>
    implements CaptchaComponentConfigManager {

  private final CaptchaComponentConfigMgrRepository componentConfigMgrRepository;
  private final MessageSourceAssertService assertService;

  @Override
  public CaptchaComponentConfig getByIdAndCheckAll(Long id) {
    CaptchaComponentConfig config = componentConfigMgrRepository.getById(id);

    assertService.isNull(
        config,
        "captcha.captcha-component-config.is-null",
        new Object[]{id},
        "[{0}]CaptchaComponentConfigId,数据不存在");

    assertService.isFalse(
        CoreConstant.DELETE_FLAG_NOT.equals(config.getDeleteFlag()),
        "captcha.captcha-component-config.is-deleted",
        new Object[]{id},
        "[{0}]CaptchaComponentConfigId,数据不存在");

    return config;
  }

  @Override
  public CaptchaComponentConfig getById(Long id) {
    return this.componentConfigMgrRepository.getById(id);
  }
}
